<template>
    <el-row class="user-info-form" type="flex" justify="center">
        <el-col>
            <div class="import-min-fire-station-wrap">
                <el-form label-width="150px"
                    :model="importMinStationForm"
                    ref="importMinStationForm"
                    @submit.native.prevent>
                    <el-form-item label="联网单位：" prop='organizationId'>
                        <el-select filterable v-model="organizationId" @change="newActionUrl">
                            <el-option
                                v-for="item in orgList"
                                :key="item.organizationId"
                                :label="item.shortName"
                                :value="item.organizationId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="文件：" prop='pictures' v-if="isShow">
                        <el-upload
                            class="upload-asset"
                            ref="upload"
                            drag
                            name="excelfile"
                            accept=".xls,.xlsx"
                            :action="actionUrl"
                            :before-upload="beforeUpload"
                            :on-preview="handlePreview"
                            :on-remove="handleRemove"
                            :on-success="handleSuccess"
                            :on-error="handleError"
                            :file-list="fileList"
                            :auto-upload="false"
                            multiple>
                            <i class="el-icon-plus"></i>
                            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                            <div class="el-upload__tip" slot="tip">*只能上传xls/xlsx文件</div>
                        </el-upload>
                        <el-row class="download-temp-wrap">
                            <span>请使用模板来上传你要添加的消火栓</span><a :href="downloadTempUrl">下载模板</a>
                        </el-row>
                    </el-form-item>
                    <el-form-item label-width="0" class="verifyidentity-button">
                        <el-button @click.stop="closeFun" round>取消</el-button>
                        <el-button type="primary" @click="confirm()" round>确定</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-col>
    </el-row>
</template>
<script>
import { getOrgList } from '@/api/org';
import { setTimeout } from 'timers';

export default {
 	name:"importMinFile",
 	props: {
        closeFun: {
            type: Function,
            required: true,
        }
    },
    data() {
    	return {
    		fileList: [],
    		orgList: [],
    		organizationId:'',
    		actionUrl: '',
    		importMinStationForm: {},
    		isShow: true,
        downloadTempUrl: this.GLOBAL.baseURL + 'municipalHydrant/downATemplate',
    	}
    },
    created() {
    	var that = this;

        // 获取业主单位信息
        getOrgList({
            queryType: 0
        }).then(res => {
            if(res.status = 200) {
                that.organizationId = res.data[0].organizationId
                that.actionUrl=this.GLOBAL.baseURL + 'municipalHydrant/addMunicipalHydrants?orgId='+ that.organizationId
                that.orgList = res.data
            }
        });
    },
    methods:{
 	    newActionUrl(){
        var that = this
        that.actionUrl=this.GLOBAL.baseURL + 'municipalHydrant/addMunicipalHydrants?orgId='+ that.organizationId
      },
    	 beforeUpload(file) {
    	 	var testmsg = file.name.substring(file.name.lastIndexOf('.')+1);
    	 	const isXSL = testmsg === 'xls'
    	 	const isXLSX = testmsg === 'xlsx'
    	 	if (!isXSL && !isXLSX) {
    	 		this.$message({
    	 			message: '上传文件只能是 xls、xlsx格式!',
    	 			type: 'warning'
    	 		});
    	 		return false;
    	 	}
    	},
    	handleRemove(file, fileList) {
    		// console.log(file, fileList)
    	},
    	handlePreview(file) {
    		// console.log(file);
    	},
    	handleSuccess(res) {
            if(res.status === 200) {
                this.$message.success("文件上传成功！")
            } else {
                this.$message.warning(res.info)
            }
    		this.closeFun(true);
    	},
    	handleError(err, file, fileList) {
    		this.$message.error("文件上传失败！");
        },
    	confirm() {
    		var that = this;
    		that.$refs.upload.submit();
    		that.closeFun(true);
    	}
    }
 }
</script>
